﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eStock.DataObjects
{
    public abstract class DataCollection<T> : CollectionBase
    {
        public T this[int index]
        {
            get { return ((T)List[index]); }
            set { List[index] = value; }
        }
        
        public int Add(T value)
        {
            return (List.Add(value));
        }

        public int Length
        {
            get { return List.Count; }
        }
        public int IndexOf(T value)
        {
            return (List.IndexOf(value));
        }

        public void Insert(int index, T value)
        {
            List.Insert(index, value);
        }

        public abstract T GetById(int id);

        /// <summary>
        /// Compares the ID of the old and new value and replaces the found
        /// old value with the new given
        /// </summary>
        /// <param name="newValue">The new value which replaces the old.
        /// ID of old and new value have to be the same</param>
        public abstract void Replace(T newValue);

        public void Remove(T value)
        {
            List.Remove(value);
        }

        public bool Contains(T value)
        {
            // If value is not of type Int16, this will return false.
            return (List.Contains(value));
        }

        protected override void OnInsert(int index, Object value)
        {
            // Insert additional code to be run only when inserting values.
        }

        protected override void OnRemove(int index, Object value)
        {
            // Insert additional code to be run only when removing values.
        }

        protected override void OnSet(int index, Object oldValue, Object newValue)
        {
            // Insert additional code to be run only when setting values.
        }

        protected override void OnValidate(Object value)
        {
            if (value.GetType() != typeof(T))
                throw new ArgumentException("value must be of type " + typeof(T).Name +".", "value");
        }

        public abstract bool Update();
    }
}
